<template>
  <div id="target">
    <div class="collectionTitle">
      <div>{{$t('userAdmin.collection.title')}}</div>
    </div>
    <el-table
      v-if="noResponse"
      v-loading="loading"
      style="width: 100%;height: 419px;margin: 10px auto;z-index: 1">
    </el-table>
    <el-empty v-if="isEmpty" :image-size="200"></el-empty>
    <div class="collectionBox" v-if="!isEmpty" v-for="(item, index) in contentData" :key="index">
      <router-link target="_blank" :to="{path:'/myselfArticle', query:{ userReplyId : item.collectionReplyId }}">
        <div class="title">
          {{item.collectionTitle}}
        </div>
      </router-link>
      <div class="details">
        <div class="titleAuthor">
          <div>
            <router-link target="_blank" :to="{path:'/userHomePage',query:{userId:item.titleAuthorId}}" :title="$t('userAdmin.collection.titleAuthor')">
              <img :src=item.titleAuthorHeadPhotoPath alt="" width="22px" style="border-radius: 50%;">
            </router-link>
          </div>
          <div>{{item.titleAuthorNickname}}</div>
        </div>
        <div class="contentAuthor">
          <div>
            <router-link target="_blank" :to="{path:'/userHomePage',query:{userId:item.replyTitleAuthorId}}" :title="$t('userAdmin.collection.contentAuthor')">
              <img :src=item.replyTitleAuthorHeadPhotoPath alt="" width="22px" style="border-radius: 50%;">
            </router-link>
          </div>
          <div>{{item.replyTitleAuthorNickname}}</div>
        </div>

        <div class="originLink">
          <router-link target="_blank" :to="{path:'/WorksDisplayPage', query:{ workId : item.originLink }}" :title="$t('userAdmin.collection.articleLink')"><i class="iconfont icon-lianjie" style="font-size: 20px;"></i></router-link>
        </div>
        <div class="collectionTime" :title="$t('userAdmin.collection.collectionTime')">
          <i class="iconfont icon-shoucangshijian" style="font-weight: bolder;"></i>
          {{item.date}}
        </div>
        <a href="javascript:;" :title="$t('userAdmin.collection.button')" @click="cancelCollection(item.collectionReplyId)">
          <div class="controller"><i class="iconfont icon-quxiaoshoucang1" style="font-size: 12px;"></i>{{$t('userAdmin.collection.button')}}</div>
        </a>
      </div>
    </div>
    <el-pagination
      v-if="!isEmpty && !noResponse"
      background
      layout="prev, pager, next"
      :total=pageTotal
      @current-change="handleCurrentChange(currentPage)"
      :current-page.sync = currentPage
      class="page">
    </el-pagination>
  </div>
</template>

<script>
    export default {
        name: "UserAdminMyCollection",
        data(){
            return{
                imageUrl1: [],
                imageUrl2: [],
                loading:"true",
                isEmpty:false,
                noResponse: true,
                baseURL:'http://localhost:8081/userAdmin/collection/',
                baseURL2:'http://localhost:8081/userAdmin/getPages/myCollections',
                baseURL3:'http://localhost:8081/worksDisplay/cancelCollection/',  // 取消收藏
                contentData:'',
                pageTotal:0,
                currentPage:1  // 当前页
            }
        },
        mounted(){
            this.$emit("component","myCollection")
            this.getCollectionInfo()
            this.getPages()
        },
        methods:{
            getCollectionInfo(){
                this.axios.post(this.baseURL+this.currentPage).then(res=>{
                    this.noResponse=false
                    if (res.data != "empty"){
                        this.contentData = res.data
                    }else {
                        this.isEmpty = true
                    }
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },
            getPages(){
                this.axios.post(this.baseURL2).then(res=>{
                    this.pageTotal = res.data * 10
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },
            cancelCollection(collectionID){
                this.$confirm(this.$t('userAdmin.collection.confirm.info'), {
                    confirmButtonText: this.$t('userAdmin.collection.confirm.confirm'),
                    cancelButtonText: this.$t('userAdmin.collection.confirm.cancel'),
                    showClose: false,
                    type: 'warning'
                }).then(() => {
                    this.axios.post(this.baseURL3+collectionID).then(res=>{
                        if (res.data == true) {
                            this.successOpt(this.$t('userAdmin.collection.confirm.successTip'))
                            this.getCollectionInfo()
                        } else {
                            this.failOpt(this.$t('userAdmin.collection.confirm.faiTip'))
                        }
                    }).catch(
                        exception=>{
                            console.log(exception)
                        }
                    )
                }).catch(() => {
                    console.log(this.$t('userAdmin.collection.confirm.cancelTip'))
                });
            },
            handleCurrentChange(currentPage){
                this.axios.post(this.baseURL+currentPage).then(res=>{
                    this.noResponse=false
                    if (res.data != "empty"){
                        this.contentData = res.data
                        let target = document.getElementById("target").offsetTop
                        window.scrollTo({
                            top:target,
                            behavior: "smooth"
                        });
                    }
                }).catch(
                    exception=>{
                        console.log(exception)
                    }
                )
            },
            successOpt(message) {
                this.$notify({
                    title: this.$t('notify.success'),
                    message: message,
                    type: 'success'
                })
            },
            failOpt(message) {
                this.$notify.error({
                    title: this.$t('notify.error'),
                    message: message
                });
            }

        }
    }
</script>

<style scoped>
  .collectionBox{
    margin: 10px auto;
    width: 100%;
    min-height: 50px;
    background-color: rgba(255, 255, 255, 0.9);
    border-radius: 3px;
    border: 1px solid rgb(229, 229, 229);
    letter-spacing: 1.5px;
    transition: 0.2s;
  }
  .collectionTitle{
    width: 100%;
    background-color: white;
    margin-bottom: 10px;
    border-radius: 3px;
  }
  .collectionTitle div{
    height: 50px;
    line-height: 50px;
    font-size: 18px;
    color: #414141;
    margin-left: 20px;
  }
  .collectionBox:hover{
    box-shadow: 0 0 5px rgb(196, 196, 196);
    transform:translateY(-1px);
  }
  .title{
    width: 890px;
    min-height: 20px;
    padding: 10px 20px;
    padding-bottom: 5px;
    font-size: 15px;
    color: rgb(78, 78, 78);
    line-height: 20px;
  }
  .details{
    width: 930px;
    height: 40px;
  }
  .details div{
    float: left;
  }
  .titleAuthor, .contentAuthor{
    max-width: 170px;
    height: 30px;
    line-height: 30px;
    margin-top: 5px;
    margin-left: 20px;
  }
  .titleAuthor div:nth-child(1),.contentAuthor div:nth-child(1){
    float: left;
    height: 30px;
    margin-top: 3px;
  }
  .contentAuthor div:nth-child(2),.titleAuthor div:nth-child(2){
    max-width: 135px;
    font-size: 13px;
    padding-left: 5px;
    color: rgb(141, 141, 141);

    display: -webkit-box;
    overflow:hidden;
    text-overflow: ellipsis;
    -webkit-box-orient:vertical;
    -webkit-line-clamp:1;
  }
  .originLink{
    width: 30px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    margin-top: 5px;
    margin-left: 50px;
  }
  .originLink a i{
    font-size: 18px;
    color: green;
  }
  .originLink a i:hover{
    color: rgb(0, 177, 0);
  }
  .collectionTime{
    width: 200px;
    height: 30px;
    line-height: 30px;
    color: rgb(141, 141, 141);
    font-size: 14px;
    margin-top: 5px;
    margin-left: 50px;
  }
  .controller{
    width: 80px;
    height: 25px;
    line-height: 25px;
    text-align: center;
    color: rgb(141, 141, 141);
    font-size: 12px;
    margin-top: 5px;
    border-radius: 3px;
    background-color: transparent;
    border: 1px solid rgb(224, 224, 224);
    position: absolute;
    right: 20px;
    /*margin-left: 220px;*/
  }
  .controller:hover{
    color: rgb(235, 235, 235);
    background-color: rgb(71, 71, 71);
    border: 1px solid rgb(43, 43, 43);
  }
  .page{
    margin: 10px auto;
    margin-left: -10px;
  }
</style>
